package com.translate.web.service.impl;

import com.translate.web.config.contract.IOSSConfig;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.NewsMapper;
import com.translate.entity.News;
import com.translate.vo.contract.INewsVO;
import com.translate.web.service.INewsService;
import com.common.util.CopyEntity;
import com.page.PageResponseData;
import com.param.contract.IPage;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class NewsServiceImpl extends BaseTranslateServiceImpl<News> implements INewsService {
	
	private IOSSConfig config;
	
	@Resource
	public void setConfig(IOSSConfig config) {
		this.config = config;
	}
	
	public NewsServiceImpl(NewsMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(News data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(News data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<News> getEntityClass() {
		return News.class;
	}

	@Resource
	public void setRedisTemplate(RedisTemplate<String, ? extends Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
	@Override
	public <VO extends INewsVO> List<VO> getListFromCache(Class<VO> target) {
		return this.entityConvertToVo(this.listToCache( RedisConstant.NEWS_KEY, 3), target);

	}
	
	protected void parseAfter(List<News> data) {
		
		String url = "https://".concat(config.getBucketName()).concat(".").concat(config.getEndpoint());
		
		for (News banner : data) {
			banner.setCover(url.concat(banner.getCover()));
		}
	}
	


	@Override
	public <S extends IPage> PageResponseData<News> getDataByList(S pageParam) {
		// TODO Auto-generated method stub
		PageResponseData<News> dataByList = super.getDataByList(pageParam);
		
		
		if (0 == dataByList.getTotal()) {
			return dataByList;
		}
		
		this.parseAfter(dataByList.getData());
		
		return dataByList;
	}

	@Override
	protected String sqlByGetPageList(String condition) {
		// TODO Auto-generated method stub
		return super.sqlByGetPageList(" and enabled = 1 and del = 0 order by create_time desc");
	}

	@Override
	protected String sqlByGetCount(String condition) {
		// TODO Auto-generated method stub
		return super.sqlByGetCount(" enabled = 1 and del = 0");
	}

	@Override
	public <VO extends INewsVO> VO getArticleById(int id, Class<VO> target) {
		News articleDetailToCache = this.getArticleDetailToCache(id, redisTemplate,
				RedisConstant.getNewsById(id));
		
		if (null == articleDetailToCache) {
			return null;
		}
		
		return CopyEntity.copyEntity(articleDetailToCache, target);
	}
	

	/**
	 * 分页查询
	 * 
	 * @param <S>
	 * @param param
	 * @param statement
	 * @throws SQLException
	 */
	protected <S> void bindParam(S param, PreparedStatement statement) throws SQLException {

		IPage pageParam = (IPage) param;
		
		pageParam.setSelectCount(4);

		statement.setInt(1, pageParam.getLastId());

		statement.setInt(2, pageParam.getSelectCount());
	}
	
}